package cn.vc.shop.controller;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.shop.entity.dto.ShopDto;
import cn.vc.shop.service.ShopService;
import cn.vc.shop.util.SecurityUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 商铺接口
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    private final ShopService shopService;
    private final RedisTemplate redisTemplate;
    private final HttpServletRequest request;

    public ShopController(ShopService shopService, RedisTemplate redisTemplate, HttpServletRequest request) {
        this.shopService = shopService;
        this.redisTemplate = redisTemplate;
        this.request = request;
    }



    /**
     * 商铺认证
     */
    @PostMapping
    public Result<?> authentication() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
//        amqpTemplate.convertAndSend(ShopRabbitMqConfig.SHOP_DIRECT_EXCHANGE, ShopRabbitMqConfig.SHOP_APPLY_KEY, myUser.getId());
//        return resultBuilder.returnMsg(1, null, "认证已提交至审核！");
        return new Result<>(1, null, "认证已提交至审核！");
    }



    /**
     * 商铺注销
     */
    @DeleteMapping
    public Result<?> signOut() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        boolean bo = shopService.signOut(myUser.getShopId());
//        return resultBuilder.returnMsg(bo?1:0, null, "商铺注销：" + myUser.getId() + " -> " + myUser.getShopId() + " : " + bo);
        return new Result<>(bo?1:0, null, "商铺注销：" + myUser.getId() + " -> " + myUser.getShopId() + " : " + bo);
    }

    /**
     * 查询我的商铺
     * @return
     */
    @GetMapping("/0")
    public Result<?> selectMyShop() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        ShopDto shopDto = shopService.selectByUserId(myUser.getId());
        return new Result<>(1, shopDto);
    }

    /**
     * 查询用户ID商铺
     * @param id 商铺ID
     * @return
     */
    @GetMapping("/1")
    public Result<?> selectById(@RequestParam Long id) {
        ShopDto shopDto = shopService.selectByUserId(id);
        return new Result<>(1, shopDto);
    }


}
